﻿using AutoMapper;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.IServices.IBasic;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PowerHub.MES.Services.Basic
{
    /// <summary>
    /// 角色菜单接口实现层
    /// </summary>
    public class BasicData_Roleinfo_MenuInfoServices : BaseServices<BasicData_Roleinfo_MenuInfo>, IBasicData_Roleinfo_MenuInfoServices
    {
        private IBasicData_Roleinfo_MenuInfoRepository _iBasicData_Roleinfo_MenuInfoRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public BasicData_Roleinfo_MenuInfoServices(PowerHubMesContext powerHubMesContext, IBasicData_Roleinfo_MenuInfoRepository iBasicData_Roleinfo_MenuInfoRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iBasicData_Roleinfo_MenuInfoRepository = iBasicData_Roleinfo_MenuInfoRepository;
            _userServices = userServices;
            _mapper = mapper;
        }


        /// <summary>
        /// 添加角色菜单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<OperationResult<bool>> CreateBasicData_Roleinfo_MenuInfoAsync(BasicData_Roleinfo_MenuInfoDTO query)
        {
            if (string.IsNullOrEmpty(query.RoleinfoId))
            {
                throw new Exception("请重新选择角色");
            }


            //根据角色获取菜单信息
            var roleinfo_MenuInfo = await GetEntityListAsync(m => m.RoleinfoId.Equals(query.RoleinfoId) && m.IsDel == 0);
            var menuInfos = roleinfo_MenuInfo.Select(m => m.MenuInfoId).ToList();

            // query.MenuInfoIds与 menuInfos 的 差集(绑定新菜单)
            List<string> createMenuInfos = query.MenuInfoIds.Except(menuInfos).ToList();
            List<BasicData_Roleinfo_MenuInfo> createRoleinfo_MenuInfos = createMenuInfos
                .Select(menuInfo => new BasicData_Roleinfo_MenuInfo
                {
                    Id = Guid.NewGuid().ToString(),
                    MenuInfoId = menuInfo,
                    Creator = _userServices.UserName,
                    RoleinfoId = query.RoleinfoId
                })
                .ToList();


            //menuInfos与 query.MenuInfoIds 的 差集 （取消绑定）
            List<string> deleteMenuInfos = menuInfos.Except(query.MenuInfoIds).ToList();
            var deleteRoleinfo_MenuInfos = roleinfo_MenuInfo.Where(m => deleteMenuInfos.Contains(m.MenuInfoId)).ToList();


            return await _iBasicData_Roleinfo_MenuInfoRepository.CreateBasicData_Roleinfo_MenuInfoAsync(createRoleinfo_MenuInfos, deleteRoleinfo_MenuInfos, query.RoleinfoId);
        }
    }
}
